import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:loading_animation_widget/loading_animation_widget.dart';
import 'package:winkai/entity/chat_msg.dart';
import 'package:winkai/entity/msg_constant.dart';
import 'package:winkai/entity/subscribe_from.dart';
import 'package:winkai/helper/iap/iap_helper.dart';
import 'package:winkai/res/app_colors.dart';
import 'package:winkai/ui/msg/widget/item/chat_msg_align_widget.dart';
import 'package:winkai/ui/msg/widget/item/chat_msg_bg_widget.dart';
import 'package:winkai/utils/image_utils.dart';
import 'package:winkai/utils/net_image_load_utils.dart';
import 'package:winkai/utils/page_jump_utils.dart';

class ChatMsgTextWidget extends StatelessWidget {
  const ChatMsgTextWidget({
    super.key,
    required this.message,
    this.onRetryClick,
    this.onAvatarClick,
  });

  final ChatMsg message;
  final void Function()? onRetryClick;
  final void Function()? onAvatarClick;

  @override
  Widget build(BuildContext context) {
    var isSend = message.isSend;
    var isPrivateChat = message.messageInfo.msg.isPrivateChat;
    var status = message.messageInfo.msg.status;
    var isHide = !isSend && !IAPHelper().isVip && isPrivateChat;
    return ChatMsgAlignWidget(
      isSend: isSend,
      children: [
        if (!isSend)
          GestureDetector(
            onTap: () {
              onAvatarClick?.call();
            },
            child: NetImageLoadUtils.loadCircle(
                message.messageInfo.user?.avatar, 40.w),
          ),
        if (!isSend) SizedBox(width: 8.w),
        if (isSend && status != MsgStatus.sended)
          status == MsgStatus.sending
              ? Padding(
                  padding: EdgeInsets.only(right: 8.w),
                  child: LoadingAnimationWidget.inkDrop(
                      color: Colors.white, size: 16.w),
                )
              : GestureDetector(
                  onTap: () {
                    onRetryClick?.call();
                  },
                  child: Padding(
                    padding: EdgeInsets.only(right: 4.w),
                    child: ImageUtils.getImage(
                        'ic_ai_chat_tips_black', 24.w, 24.w),
                  ),
                ),
        Flexible(
          child: ChatMsgBgWidget(
            isSend: isSend,
            child: isHide ? _buildUnlockWidget(context) : _buildContent(),
          ),
        )
      ],
    );
  }

  Widget _buildContent() {
    var isSend = message.isSend;
    return Text(
      message.type == ChatMsg.typeText
          ? message.content ?? ''
          : 'chat_unknown_message'.tr,
      style: TextStyle(
        color: isSend ? Colors.black : AppColors.color_4d,
        fontSize: 14.sp,
        fontWeight: FontWeight.w500,
      ),
    );
  }

  Widget _buildUnlockWidget(BuildContext context) {
    var isSend = message.isSend;
    return GestureDetector(
      onTap: () {
        PageJumpUtils.toVipPage(SubscribeFrom.lockText);
      },
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Text(
            '************',
            style: TextStyle(
              color: isSend ? Colors.black : AppColors.color_4d,
              fontSize: 14.sp,
              fontWeight: FontWeight.w500,
            ),
          ),
          Padding(
            padding: EdgeInsets.only(left: 4.w),
            child: ImageUtils.getImage('ic_ai_unlock2', 20.w, 20.w),
          )
        ],
      ),
    );
  }
}
